﻿using System;
using System.Globalization;
using System.Web.UI;
using Diploma.Models;
using Diploma.Utils;
using DiplomaWeb.Configuration;
using IZ.WebFileManager;

namespace DiplomaWeb.Views
{
    public partial class FileManagerForm : Page
    {
        private Lazy<DiplomaConfiguration> configuration = new Lazy<DiplomaConfiguration>(() => new DiplomaConfiguration());

        protected void Page_Load(object sender, EventArgs e)
        {
            string userId = Request.QueryString["userId"];
            string roomId = Request.QueryString["roomId"];
            if (userId == null || roomId == null)
                return;
            User user = DataManager.Instance.UsersRepository.GetUserById(Guid.Parse(userId));
            CollaborationRoom room =
                DataManager.Instance.CollaborationRoomRepository.GetCollaborationRoom(Guid.Parse(roomId));
            if (user == null || room == null)
                return;
            if (room.AllowAccess(user))
            {
                fm.Height = 350;
                fm.Width = 550;

                string directoryPath = String.Format(CultureInfo.CurrentCulture,
                    configuration.Value.Settings.CollaborationRoomsContentPath,
                    room.CollaborationRoomId);

                //string directoryPath = ConstantsUtil.GetRoomDirectoryPath(PathType.Virtual) +
                //                       room.CollaborationRoomId.ToString();

                fm.RootDirectories.Clear();
                var rootDirectory = new RootDirectory();
                rootDirectory.DirectoryPath = directoryPath;
                rootDirectory.Text = "My Files";
                fm.RootDirectories.Add(rootDirectory);

                fm.Visible = true;
            }
            else
            {
                fm.Visible = false;
            }
        }
    }
}